package com.chaos.auth.service;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.chaos.framework.model.dto.ID;
import com.chaos.framework.model.dto.page.PageList;
import com.chaos.framework.model.dto.page.PageQueryDto;
import com.chaos.framework.model.entity.Table;
import com.chaos.framework.model.helper.ConvertHelper;
import com.chaos.framework.model.helper.PageHelper;
import com.chaos.auth.entity.RbacPermission;
import com.chaos.auth.api.data.RbacPermissionData;
import com.chaos.auth.mapper.RbacPermissionMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chaos.framework.model.serivce.RedisService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import com.chaos.framework.model.constant.SecurityConstants;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Jacky.Cui
 * @since 2024-11-13
 */
@Slf4j
@Component
public class RbacPermissionService extends ServiceImpl<RbacPermissionMapper, RbacPermission> {

    @Resource
    private RedisService redisService;

    public ID insertModel(RbacPermissionData data) {
        RbacPermission entity = ConvertHelper.convert(data, RbacPermission.class);
        return save(entity) ? new ID(entity.getId()) : null;
    }

    public boolean deleteModel(ID data) {
        return removeById(data.getId());
    }

    public boolean updateModelByID(RbacPermissionData data) {
        RbacPermission entity = ConvertHelper.convert(data, RbacPermission.class);
        return update(entity, new UpdateWrapper<RbacPermission>().eq(Table.ID, data.getId()));
    }

    public RbacPermissionData selectByID(ID data) {
        return ConvertHelper.convert(getOne(new QueryWrapper<RbacPermission>().eq(Table.ID, data.getId())), RbacPermissionData.class);
    }

    public List<RbacPermissionData> selectByData(RbacPermissionData data) {
        QueryWrapper<RbacPermission> query = new QueryWrapper<>();
        query.lambda()
                .eq(!ObjectUtil.isEmpty(data.getId()), RbacPermission::getId, data.getId())
                .eq(!ObjectUtil.isEmpty(data.getName()), RbacPermission::getName, data.getName())
                .eq(!ObjectUtil.isEmpty(data.getUrlPerm()), RbacPermission::getUrlPerm, data.getUrlPerm())
                .eq(!ObjectUtil.isEmpty(data.getType()), RbacPermission::getType, data.getType())
        ;
        query.orderByDesc(Table.ID);
        return ConvertHelper.convert(list(query), RbacPermissionData.class);
    }

    public PageList<RbacPermissionData> selectByPage(PageQueryDto<RbacPermissionData> pageData) {
        QueryWrapper<RbacPermission> query = new QueryWrapper<>();
        query.orderByDesc(Table.ID);
        return PageHelper.pageList(page(PageHelper.page(pageData), query), RbacPermissionData.class);
    }

    public void initAdminPermission() {
        List<RbacPermissionData> rolePermissionList = baseMapper.rolePermissionList();
        if (CollectionUtil.isEmpty(rolePermissionList)) {
            return;
        }
        Map<String, List<String>> rolePermissionMap = rolePermissionList.stream().collect(Collectors.groupingBy(RbacPermissionData::getUrlPerm, Collectors.mapping(RbacPermissionData::getRoleName, Collectors.toList())));
        redisService.hMset(SecurityConstants.AUTH_ROLE_PERMISSION_URL_KEY, rolePermissionMap);

    }
}
